# -*- coding: utf-8 -*
# @author 小测试
# @create 2020-03-03 22:44
from ApiDemo.util.get_auto_data import atuo_case
from ApiDemo.util.tools.open_ini import Config
from ApiDemo.util.get_assert import Assertions
from ApiDemo.util.get_method import GetMethod
from ApiDemo.util.get_head import header
from ApiDemo.util.get_log import MyLog
import allure
import pytest
import json

log = MyLog
HOST = Config().host_test
# XLSPATH="use_api_case.xls"
# JSONPATH="user_api_data.json"
JSONPATH = r"F:\Env\apidemo\ApiDemo\cases\user\user_api_data.json"  # get_json_path(JSONNAME)
XLSPATH = r"F:\Env\apidemo\ApiDemo\cases\user\use_api_case.xls"  # get_xls_path(XLSNAME)

CASE_DATA = atuo_case(json_path=JSONPATH, excle_path=XLSPATH)
config = Config()

##冒烟测试
@pytest.mark.smoke
@pytest.mark.parametrize("params", CASE_DATA[0])
# api_name,case_title,api,method,head,data,case_expect
@allure.feature("用户管理")
def test_user_smoke(params, login):
    # 报告
    api_name = params[0]  # 接口名字
    case_doc = params[1]  # 接口的描述信息

    allure.dynamic.story(api_name)  # 向报告描述中添加接口名字
    allure.dynamic.description(case_doc)  # 向报告中的case添加描述
    # api信息
    api = params[2]  # api
    method = params[3]  # 请求方法,post等
    head = params[4]  # head,空或者是token
    data = json.dumps(params[5])  # 请求数据,dict转json
    case_expect = params[6]  # 预期结果,dict
    url = HOST + api
    if head == "token":
        head = header(login)
    else:
        head = header()
    log.info("正在执行{}的{}用例".format(api_name, case_doc))
    with allure.step("第一步,发送请求,请求数据是:\n{}".format(data)):
        res = GetMethod().run_method(method=method,
                                     url=url,
                                     header=head,
                                     data=data)
    with allure.step("第二步,获取响应,响应结果是:\n{}".format(res)):
        Assertions().run(res, case_expect)


@pytest.mark.test
@pytest.mark.parametrize("params", CASE_DATA[1])
# api_name,case_title,api,method,head,data,case_expect
@allure.feature("用户管理")
def test_user_test(params, login):
    # 报告
    api_name = params[0]  # 接口名字
    case_doc = params[1]  # 接口的描述信息

    allure.dynamic.story(api_name)  # 向报告描述中添加接口名字
    allure.dynamic.description(case_doc)  # 向报告中的case添加描述
    # api信息
    api = params[2]  # api
    method = params[3]  # 请求方法,post等
    head = params[4]  # head,空或者是token
    data = json.dumps(params[5])  # 请求数据,dict转json
    case_expect = params[6]  # 预期结果,dict
    url = HOST + api
    if head == "token":
        head = header(login)
    else:
        head = header()
    log.info("正在执行{}的{}用例".format(api_name, case_doc))
    with allure.step("第一步,发送请求,请求数据是:\n{}".format(data)):
        res = GetMethod().run_method(method=method,
                                     url=url,
                                     header=head,
                                     data=data)
    with allure.step("第二步,获取响应,响应结果是:\n{}".format(res)):
        Assertions().run(res, case_expect)

if __name__ == '__main__':
    test_user_smoke()
